package com.oms.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.oms.common.PageableVO;
import com.oms.module.admin.constant.AnnouncementOrderEnum;
import com.oms.module.admin.domain.entity.AnnouncementEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author Zhang HaiLong QQ:38592077
 * @Description TODO
 * @Date 20:33 2020/3/28
 */
public class OrderEncapsulation {
    public static void order(@Validated @RequestBody PageableVO<AnnouncementOrderEnum> pageableVO, QueryWrapper<AnnouncementEntity> query) {
        List<String> ascCollect = (List<String>)pageableVO
                .getSortList()
                .stream()
                .filter( t -> t.getOrder().getValue() == true )
                .map( t -> (String)t.getSortProperty().getValue() )
                .collect( Collectors.toList() );
        List<String> descCollect = (List<String>)pageableVO
                .getSortList()
                .stream()
                .filter( t -> t.getOrder().getValue() == true )
                .map( t -> (String)t.getSortProperty().getValue() )
                .collect( Collectors.toList() );
        String[] strArray = new String[ascCollect.size()];
        query.orderByAsc( ascCollect.toArray(strArray) );
        strArray = new String[descCollect.size()];
        query.orderByDesc( descCollect.toArray(strArray) );
    }
}
